package servlet;


import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("sex");
        String profession = request.getParameter("occupation");

       String description=request.getParameter("description");


        String[] favor = request.getParameterValues("interest"); // 获取多个参数值
        String favorString;
        if (favor != null && favor.length > 0) {
            // 将多个参数值拼接成一个字符串，以逗号分隔拼接多个值
            favorString = String.join(",", favor);
        } else {
            favorString = ""; // 如果未获取到任何参数值，可以设置为空字符串或其他默认值
        }

        if (is_register(username)) { // 检查用户名是否已注册
            boolean registrationSuccess = register(username, password, gender, profession, favorString, description);
            if (registrationSuccess) {
                // 注册成功，重定向到注册成功页面
                response.sendRedirect("register/register_success.jsp");
            } else {
                // 注册失败，重定向到注册失败页面
                response.sendRedirect("register/register_errorpage.jsp");
            }
        } else {
            // 用户名已存在，重定向到用户名已存在页面
            response.sendRedirect("register/register_exists.jsp");
        }


    }

    private boolean is_register(String username) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql="select * from 02_38_yangyang where username=? ";
        ResultSet resultSet = jdbcUtil.prepareQuery(sql,username);

        try {

            if (!resultSet.next()) {
                // 登录成功
                return true;
            } else {
                // 登录失败，账号不存在

                return false;
            }
        } catch (SQLException e) {
            // 处理异常
            e.printStackTrace();
            return false; // 或者抛出自定义异常
        } finally {
            // 关闭数据库资源
            jdbcUtil.closeConnection();
        }
    }
    public boolean register(String username, String password, String gender, String profession, String favor, String description) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "INSERT INTO 02_38_yangyang(username, password, gender, profession, favor, description) VALUES (?,?,?,?,?,?)";

        // 执行插入操作，传入参数并获取操作结果
        boolean registrationSuccess = jdbcUtil.update(sql, username, password, gender, profession, favor, description);

        // 返回操作结果
        return registrationSuccess;
    }

}